﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;
namespace GiaoDien
{
    public partial class FormTraCuuSach : Form
    {
        private DataTable datatable;
        private string sqlquery;
        public FormTraCuuSach()
        {
            InitializeComponent();
        }

        private void init()
        {
            datatable = new DataTable();
            sqlquery = "";
        }

        private void Loadsach()
        {
            SACHBUS sBUS = new SACHBUS();
            datatable = sBUS.Bangsach();
            if (datatable.Rows.Count != 0)
            {
                AutoCompleteStringCollection auto = new AutoCompleteStringCollection();
                tbtensach.AutoCompleteMode = AutoCompleteMode.Append;
                tbtensach.AutoCompleteSource = AutoCompleteSource.CustomSource;
                IDataReader read = datatable.CreateDataReader();
                while (read.Read())
                {
                    auto.Add(read["TenSach"].ToString());
                }
                tbtensach.AutoCompleteCustomSource = auto;
            }
        }

        private void Loadtheloaisach()
        {
            THELOAISACHBUS tlBUS = new THELOAISACHBUS();
            DataTable data = tlBUS.Loadtheloaisach();
            cbbtheloai.DataSource = data;
            cbbtheloai.DisplayMember = "TenTheLoai";
            cbbtheloai.ValueMember = "MaLoaiSach";
            if (data.Rows.Count != 0)
                cbbtheloai.SelectedIndex = 0;
        }

        private void Loadtacgia()
        {
            TACGIABUS tgBUS = new TACGIABUS();
            DataTable data = tgBUS.Loadtacgia();
            if (data.Rows.Count != 0)
            {
                AutoCompleteStringCollection auto = new AutoCompleteStringCollection();
                tbtacgia.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                tbtacgia.AutoCompleteSource = AutoCompleteSource.CustomSource;
                IDataReader read = data.CreateDataReader();
                while (read.Read())
                {
                    auto.Add(read["TenTacGia"].ToString());
                }
                tbtacgia.AutoCompleteCustomSource = auto;
            }
        }

        private void FormTraCuuSach_Load(object sender, EventArgs e)
        {
            init();
            Loadsach();
            Loadtheloaisach();
            Loadtacgia();
        }

        private void btntracuu_Click(object sender, EventArgs e)
        {
            string masach = tbmasach.Text.Trim().ToUpper();
            string tensach = tbtensach.Text.Trim().ToUpper();
            string matheloai = cbbtheloai.SelectedValue.ToString();
            string tentacgia = tbtensach.Text.Trim().ToUpper();
            DataView dataview = new DataView(datatable);
            TACGIABUS tgBUS = new TACGIABUS();
            int matacgia = tgBUS.laymatacgia(tentacgia);
            string filter = "MaSach like '%" + masach + "%'";
            filter += " OR TenSach like '%" + tensach + "%'";
            filter += " OR MaTheLoai like '%" + matheloai + "%'";
            filter += " OR MaTacGia like '%" + matacgia.ToString() + "%'";
            dataview.RowFilter = filter;
            dataGridView1.DataSource = dataview;
        }

        private void btnxemtatca_Click(object sender, EventArgs e)
        {
            this.dataGridView1.DataSource = datatable;
        }

        private void btnthoat_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
